package com.v.im.websocketservice.service;

import com.alibaba.fastjson.JSON;
import com.v.im.commonmodule.utils.chat.Message;
import com.v.im.websocketservice.bean.SocketUserInfo;
import com.v.im.websocketservice.websocket.ImWebSocket;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;

/**
 * @author wym
 * @create 2019-03-13 17:47
 **/
public class PushClientService extends WebSocketService {

    private static void sendMessage(ImWebSocket imWebSocket, String message) throws IOException {
        imWebSocket.session.getBasicRemote().sendText(message);
    }

    /**
     *  向客户端推送消息
     * @param message
     * @throws IOException
     */
    public static void sendFriendMessag1e(Message message) throws IOException {
        if (message != null && StringUtils.isNotBlank(message.getId())) {
            SocketUserInfo socketUserInfo = userInfoMap.get(message.getId());
            if (socketUserInfo != null) {
                ImWebSocket imWebSocket = socketUserInfo.getImWebSocket();
                sendMessage(imWebSocket, JSON.toJSONString(message));
            }
        }
    }


}
